// pages/lifeService/repairReport/evaluate.js
const app = getApp();
var topOpacity = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件参数设置，传递到组件
    defaultData: {
      title: "评价", // 导航栏标题
      showHome: false, //是否显示回到首页按钮
      showBack: true, //是否显示返回按钮
      showLogo: false, //是否显示logo
      bgColor: 'transparent'
    },
    baseUrl: app.baseUrl,
    version: app.globalData.version,
    navBarHeight: app.globalData.navBarHeight,
    dzList:[],
    dzList1: [{
      id: 0,
      name: '响应快速',
      choose: false
    }, {
      id: 1,
      name: '结果满意',
      choose: false
    }, {
      id: 2,
      name: '工具摆放',
      choose: false
    }, {
      id: 3,
      name: '现场保护',
      choose: false
    }, {
      id: 4,
      name: '过程反馈',
      choose: false
    }, {
      id: 5,
      name: '现场清洁',
      choose: false
    }, {
      id: 6,
      name: '仪容仪表',
      choose: false
    }, {
      id: 7,
      name: '服务态度',
      choose: false
    }, {
      id: 8,
      name: '处理结果',
      choose: false
    }],
    dzList2: [{
      id: 0,
      name: '工艺粗糙',
      choose: false
    }, {
      id: 1,
      name: '专业欠佳',
      choose: false
    }, {
      id: 2,
      name: '响应较慢',
      choose: false
    }, {
      id: 3,
      name: '鞋套穿戴',
      choose: false
    }, {
      id: 4,
      name: '工具摆放',
      choose: false
    }, {
      id: 5,
      name: '维修噪音',
      choose: false
    }, {
      id: 6,
      name: '沟通反馈',
      choose: false
    }, {
      id: 7,
      name: '现场清洁',
      choose: false
    }, {
      id: 8,
      name: '仪容仪表',
      choose: false
    }, {
      id: 9,
      name: '索要好评',
      choose: false
    }, {
      id: 10,
      name: '处理结果',
      choose: false
    }, {
      id: 11,
      name: '服务台读',
      choose: false
    }],
    chooseDzList: [],
    evaluateList: [{
      id: 0,
      name: '非常不满意',
      choose: false
    }, {
      id: 1,
      name: '不满意',
      choose: false
    }, {
      id: 2,
      name: '一般',
      choose: false
    }, {
      id: 3,
      name: '满意',
      choose: false
    }, {
      id: 4,
      name: '非常满意',
      choose: false
    }],
    chooseStarIndex: -1,
    recordInfo: '',
    showLable: false,
    userId: '',
    gdId: '',
    partPro: '',
    shareTitle: '',
    shareImg: '',
  },
  onPageScroll(t) {
    if (t.scrollTop < 40) {
      topOpacity = 0
      if (this.data.topOpacity != 0) {
        this.setData({
          topOpacity: 0
        })
      }
    } else if (t.scrollTop >= 40) {
      if (topOpacity <= 1) {
        topOpacity = t.scrollTop / 100 - .4
      } else {
        topOpacity = 1
      }
      if (topOpacity <= 1) {
        this.setData({
          topOpacity: topOpacity
        })
      }
    }
  },
  partPro: function (e) {
    this.setData({
      partPro: e.detail.value
    })
  },
  dz(e) {
    let that = this;
    let data = e.currentTarget.dataset.item;
    let dzList = that.data.dzList;
    dzList[data.id].choose = !data.choose;
    let chooseDzList = that.data.chooseDzList;
    that.setData({
      dzList
    }); 
    if (dzList[data.id].choose) {
      chooseDzList.push(dzList[data.id].name);
    } else {
      let index = chooseDzList.indexOf(dzList[data.id].name);
      if (index > -1) {
        chooseDzList.splice(index, 1);
      }
    }
    that.setData({ 
      chooseDzList
    })
  },
  //选择几颗星
  star(e) {
    let that = this;
    let item = e.currentTarget.dataset.item;
    let evaluateList = that.data.evaluateList;
    let index = item.id;
    if (index >= 0 && index <= 2) {
      that.setData({
        dzList: that.data.dzList2,
        chooseDzList:[]
      })
    } else {
      that.setData({
        dzList: that.data.dzList1,
        chooseDzList:[]
      })
    }
    if (!evaluateList[index].choose) {
      for (var i = 0; i < (index + 1); i++) {
        evaluateList[i].choose = true;
      }
      that.setData({ 
        chooseStarIndex: index
      })
    } else {
      for (var i = index; i < evaluateList.length; i++) {
        evaluateList[i].choose = false;
      }
      that.setData({
        chooseStarIndex: (index - 1)
      })
    }

    that.setData({
      evaluateList
    })

  },
  tj() {
    let that = this;
    let partPro = that.data.partPro;
    let data = {};
    data.userId = that.data.userId;
    data.id = that.data.gdId;
    if (that.data.chooseStarIndex < 0) {
      app.showTip("请对服务进行评价");
      return;
    }
    if(partPro!=""){
      data.content = partPro;
    }
    if (that.data.chooseStarIndex >= 0 && that.data.chooseStarIndex <= 2) {
      if (that.data.chooseDzList.length <= 0) {
        app.showTip("请选择标签");
        return;
      }else{
        data.labelArr = that.data.chooseDzList;
      }
    }else{
      if (that.data.chooseDzList.length > 0) {
        data.labelArr = that.data.chooseDzList;
      }
    }
    data.comfLevel = that.data.chooseStarIndex + 1;
    wx.showLoading({ 
      title: '数据提交中...' 
    }); 
    
    wx.request({
      url: that.data.baseUrl + 'api/getApiData.php',
      method: 'POST',
      data: {
        'apiUrl': '/openapi/maintOrder/v1/evaluate',
        'data': JSON.stringify(data)
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.status == 1) {
          wx.navigateTo({
            url: '/pages/lifeService/repairReport/recordList?backHome=true',
          })
          app.showTip('提交成功');
        }else{
          app.showTip(res.data.msg);
        }
      },
      fail: function (res) {
        //调用最终上传数据
        wx.hideLoading();
        app.showTip('服务器繁忙,请稍候重试');
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this;
    var openid = wx.getStorageSync('ly24_openid');
    wx.request({
      url: that.data.baseUrl + 'api/getUserId.php',
      method: 'POST',
      data: {
        'openid': openid,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.status == 1) {
          var userId = res.data.userId;
          that.setData({
            userId: userId
          });
        }else{
          app.showTip(res.data.msg);
        }
      },
      fail: function (res) {}
    });
    let id = options.id;
    var data = {
      id: id
    }
    that.setData({
      gdId: id,
      dzList:that.data.dzList2
    })
    wx.request({
      url: that.data.baseUrl + 'api/getApiData.php',
      method: 'POST',
      data: {
        'apiUrl': '/openapi/maintOrder/v1/detail',
        'data': JSON.stringify(data)
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.status == 1) {
          wx.hideLoading();
          that.setData({
            recordInfo: res.data.data
          })
        }
      },
      fail: function (res) {
        //调用最终上传数据
        wx.hideLoading();
        app.showTip('服务器繁忙,请稍候重试');
      }
    });
     //调用分享
     that.getShareInfo();
  },
//获取微信分享信息
getShareInfo() {
  var that = this;
  wx.request({
    url: that.data.baseUrl + 'getShareInfo.php',
    method: 'POST',
    data: {

    },
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    success: function (res) {
      that.setData({
        shareTitle: res.data.shareTitle,
        shareImg: res.data.shareImg
      });
    },
    fail: function (res) {}
  });
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var that = this;
    return {
      title: that.data.shareTitle,
      path: '/pages/index/index',
      imageUrl: that.data.shareImg,
      success: function (res) {

      },
      fail: function (res) {

      }
    }
  }
})